package com.csi.controller.back;


import cn.hutool.jwt.JWTUtil;
import com.csi.domain.Result;
import com.csi.domain.UserAssess;
import com.csi.service.AssessService;
import com.csi.utils.ReturnResults;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@RequestMapping("/assess")
@RestController
public class AssessController {

    @Autowired
    private AssessService assessService;

    /**
     * 新增评价
     * @param userAssess
     * @param request
     * @return
     */
    @PostMapping("/save")
    public Object save(@RequestBody UserAssess userAssess, HttpServletRequest request){
        String token =  request.getHeader("token");
        long userId = Long.parseLong(JWTUtil.parseToken(token).getPayload("userId").toString());
        userAssess.setUserId(userId);
        userAssess.setAccessDate(new Timestamp(System.currentTimeMillis()));
        int saved = assessService.save(userAssess);
        if(saved > 0){
            return new Result("新增成功", ReturnResults.ACCESS_SUCCESS);
        }
        return new Result("添加失败", ReturnResults.ACCESS_FAILED);
    }
    @RequestMapping("/find")
    public Object find(HttpServletRequest request){
        String token =  request.getHeader("token");
        long hotelId = Long.parseLong(JWTUtil.parseToken(token).getPayload("hotelId").toString());
        Map<String,Object> assessList = assessService.find(hotelId);
        if (assessList != null){
            return new Result("查找成功", ReturnResults.ACCESS_SUCCESS,assessList.values());
        }
        return new Result("该酒店无评价", ReturnResults.ACCESS_FAILED);
    }


}
